package com.beagle.njddapp.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.IntDef;


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * modified on 2018/10/17  : 将全局变量改为方法，以应对在app运行期间手机屏幕变化的情况
 */

public class LocalDisplay {

    private static final String TAG = "LocalDisplay";

    //屏幕的类型-宽屏、普通屏、窄屏
    public static final int SCREEN_TYPE_NORMAL = 0;
    public static final int SCREEN_TYPE_HIGH = 1;
    public static final int SCREEN_TYPE_WIDE = 2;

    @IntDef({SCREEN_TYPE_NORMAL, SCREEN_TYPE_HIGH, SCREEN_TYPE_WIDE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SCREEN_TYPE_ENUM {
    }

    @SuppressLint("StaticFieldLeak")
    private static Context appCtx;

    public static void init(Context context) {
        if (context != null) {
            appCtx = context.getApplicationContext();

        }
    }

    public static @SCREEN_TYPE_ENUM
    int getScreenType() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                if (dm.widthPixels > dm.heightPixels) {
                    return SCREEN_TYPE_WIDE;
                } else {
                    return isHighScreen() ? SCREEN_TYPE_HIGH : SCREEN_TYPE_NORMAL;
                }
            }
        }
        return SCREEN_TYPE_NORMAL;
    }

    public static int screenWidthPixels() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return Math.min(dm.widthPixels, dm.heightPixels);
            }
        }
        return 0;
    }

    public static int screenHeightPixels() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return Math.max(dm.heightPixels, dm.widthPixels);
            }
        }
        return 0;
    }

    public static float screenDensity() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return dm.density;
            }
        }
        return 0;
    }

    public static float screenScaledDensity() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return dm.scaledDensity;
            }
        }
        return 0;
    }

    public static int screenWidthDp() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return (int) ((float) Math.min(dm.widthPixels, dm.heightPixels) / dm.density);
            }
        }
        return 0;
    }

    public static int screenHeightDp() {
        if (appCtx != null && appCtx.getResources() != null) {
            DisplayMetrics dm = appCtx.getResources().getDisplayMetrics();
            if (dm != null) {
                return (int) ((float) Math.max(dm.heightPixels, dm.widthPixels) / dm.density);
            }
        }
        return 0;
    }

    /**
     * 当屏幕高宽比大于1920/1080，则认为是高屏手机，如vivo x21A 1080x2280
     */
    public static boolean isHighScreen() {
        if (appCtx != null) {
            WindowManager wm = (WindowManager) appCtx.getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                DisplayMetrics dm = new DisplayMetrics();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    wm.getDefaultDisplay().getRealMetrics(dm);
                } else {
                    wm.getDefaultDisplay().getMetrics(dm);
                }
                int w = Math.min(dm.widthPixels, dm.heightPixels);
                int h = Math.max(dm.widthPixels, dm.heightPixels);
                return (float) h / (float) w > 1920f / 1080f;
            }
        }
        return false;
    }

    /**
     * 状态栏高度
     */
    private static int statusBarHeight = 0;

    /**
     * 获取状态栏高度
     */
    public static int getStatusBarHeight() {

        long t1 = System.currentTimeMillis();

        if (statusBarHeight <= 0) {
            if (appCtx != null) {
                int resourceId = appCtx.getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    // 根据资源ID获取响应的尺寸值
                    statusBarHeight = appCtx.getResources().getDimensionPixelSize(resourceId);
                }
            }
        }

        long t2 = System.currentTimeMillis();

        return statusBarHeight;
    }


    public static int dp2px(float dp) {
        return (int) (dp * screenDensity() + 0.5f);
    }

    public static int px2dp(float px) {
        return (int) ((px - 0.5f) / screenDensity());
    }

    public static int sp2px(float sp) {
        return (int) (sp * screenScaledDensity() + 0.5f);
    }

    public static int designedDP2px(float designedDp) {
        if (screenWidthDp() != 320) {
            designedDp = designedDp * screenWidthDp() / 320f;
        }
        return dp2px(designedDp);
    }

    public static void setPadding(final View view, float left, float top, float right, float bottom) {
        view.setPadding(designedDP2px(left), dp2px(top), designedDP2px(right), dp2px(bottom));
    }


    /**
     * 字体设置为默认大小，不受系统设置影响
     *
     * @param res
     */
    public static void setFontToDefault(Resources res) {
        if (res != null) {
            Configuration config = res.getConfiguration();
            if (config != null) {
                config.fontScale = 1;
                res.updateConfiguration(config, res.getDisplayMetrics());
            }
        }
    }

    /**
     * 获取手机屏幕的真实高度（包含虚拟按键）
     */
    public static int getScreenHeightIncludeSystemView() {
        if (appCtx != null) {
            WindowManager wm = (WindowManager) appCtx.getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                DisplayMetrics dm = new DisplayMetrics();
                wm.getDefaultDisplay().getRealMetrics(dm);
                return Math.max(dm.widthPixels, dm.heightPixels);
            }
        }
        return 0;
    }

    /**
     * 获取手机屏幕的真实宽度（包含虚拟按键）
     */
    public static int getScreenWidthIncludeSystemView() {
        if (appCtx != null) {
            WindowManager wm = (WindowManager) appCtx.getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                DisplayMetrics dm = new DisplayMetrics();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    wm.getDefaultDisplay().getRealMetrics(dm);
                } else {
                    wm.getDefaultDisplay().getMetrics(dm);
                }
                return Math.min(dm.widthPixels, dm.heightPixels);
            }
        }
        return 0;
    }
}
